# Rakefile for the separate testing of JRuby support
# (Separate because we need to run JRuby, not MRI...)

require 'rubygems'
require 'rake'
require 'rake/clean'

# Use RSpec's files.
require 'rspec/core/rake_task'

task :default => [:jruby]

task :jruby => [:aquarium_spec, :jruby_spec] 

desc "Run all specs"
RSpec::Core::RakeTask.new do |t|
  # t.spec_files = FileList['spec/**/*_spec.rb', 'examples/**/*_spec.rb']
  t.rspec_opts = ['--colour'] + FileList['spec/**/*_spec.rb', 'examples/**/*_spec.rb']
end
task :spec => :verify_jruby

desc "Run all specs in reverse"
RSpec::Core::RakeTask.new(:spec_in_reverse) do |t|
  #t.spec_files = FileList['spec/**/*_spec.rb', 'examples/**/*_spec.rb']
  t.rspec_opts = ['--reverse', '--colour'] + FileList['spec/**/*_spec.rb', 'examples/**/*_spec.rb']
end
task :spec => :verify_jruby

desc "Run all of aquarium's specs using JRuby"
task :aquarium_spec => :verify_jruby do
  output = nil
  Dir.chdir '..' do
    sh "rake spec"
  end
end

task :verify_jruby do
  unless self.class.const_defined? "JRUBY_VERSION"
    raise "ERROR: You must run these rake tasks with jruby."
  else
    #raise "WARNING: JRuby support is not provided in this release. See ../CHANGES for details."
  end
end

CLEAN.include('java/example.jar')
CLEAN.include('java/**/*.class')
SRC = FileList['java/**/*.java']
OBJ = SRC.ext('class')

rule '.class' => '.java' do |t|
  sh "javac -cp java #{t.source}"
end

file 'java/example.jar' => OBJ do
  classes = OBJ.map {|c| c.gsub(/^java\//, '')}
  Dir.chdir 'java' do
    sh "jar -cf example.jar #{classes}"
  end
end

task :jruby_spec => ['java/example.jar', :spec, :spec_in_reverse]
